/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 - 2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/

package com.genia.toolbox.web.gwt.form.client.widget.item.impl;

import java.util.ArrayList;
import java.util.List;

import com.genia.toolbox.web.gwt.basics.client.GwtHelper;
import com.genia.toolbox.web.gwt.form.client.CssNames;
import com.genia.toolbox.web.gwt.form.client.form.SubFormItem;
import com.genia.toolbox.web.gwt.form.client.manager.FormManager;
import com.genia.toolbox.web.gwt.form.client.manager.FormSwitchCallback;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Utility class that helps using sub forms.
 */
public class SubFormHelper
{

  /**
   * start a new form.
   * 
   * @param parent
   *          the widget that ask for the creation of a new form.
   * @param formManager
   *          the {@link FormManager} currently managing the form.
   * @param subFormDataIdentifier
   *          the data identifier of the form to display.
   * @param multiple
   *          whether the widget starting the form handle multiple values.
   */
  public static void startNewForm(final AbstractSimpleItemWidget<? extends SubFormItem> parent, final FormManager formManager, final String subFormDataIdentifier, final boolean multiple)
  {
    // the item describing the sub form to start.
    final SubFormItem item = parent.getItem();

    // whether to create a popup.
    final boolean popup = item.isPopup();

    // create this dialog box as a final variable to be able to hide it in the
    // callback.
    final DialogBox dialogBox = popup ? new DialogBox() : null;

    FormSwitchCallback formSwitchCallback = (subFormDataIdentifier != null && !popup) ? null : new FormSwitchCallback()
    {

      public void switchBack(final FormManager currentManager)
      {
        if (subFormDataIdentifier == null) {
          if (currentManager.getDataIdentifier().getIdentifier() != null) {
            if (multiple) {
              if (currentManager.getDataIdentifier().getIdentifier() != null) {
                List<String> oldValues = formManager.getValues(item.getName());
                if (oldValues == null) {
                  oldValues = new ArrayList<String>();
                }
                if (!oldValues.contains(currentManager.getDataIdentifier().getIdentifier())) {
                  oldValues.add(currentManager.getDataIdentifier().getIdentifier());
                  formManager.setValues(item.getName(), oldValues);
                }
              }
            }
            else {
              formManager.setValue(item.getName(), currentManager.getDataIdentifier().getIdentifier());
            }
          }
        }
        if (popup) {
          dialogBox.hide();
        }
      }

    };

    if (popup) {
      GwtHelper.fillText(dialogBox, item.getLabelKey());
      dialogBox.addStyleName(CssNames.POPUP_PANEL_CSS_STYLE);
      final VerticalPanel mainPanel = new VerticalPanel();
      final HorizontalPanel buttonsPanel = new HorizontalPanel();
      mainPanel.addStyleName(CssNames.MAIN_PANEL_STYLE_NAME);
      buttonsPanel.addStyleName(CssNames.BUTTON_PANEL_STYLE_NAME);
      final VerticalPanel layoutPanel = new VerticalPanel();
      layoutPanel.add(mainPanel);
      layoutPanel.add(buttonsPanel);
      dialogBox.add(layoutPanel);
      dialogBox.show();
      formManager.getFormController().switchForm(mainPanel, buttonsPanel, item.getSubFormIdentifier(), formManager.getDataIdentifier().getChildDataIdentifier(subFormDataIdentifier),
          formSwitchCallback);
    }
    else {
      formManager.getFormController().switchForm(item.getSubFormIdentifier(), formManager.getDataIdentifier().getChildDataIdentifier(subFormDataIdentifier), formSwitchCallback);
    }

  }
}
